package com.first.nell.launch

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.first.nell.ProjectApplication
import com.first.nell.base.BaseViewModel
import com.first.nell.data.*
import com.first.nell.extend.isFirstNellAgreeFirstNellSpecFirstNellPermissions
import com.first.nell.network.ApiUserReposity
import com.first.nell.utils.AppConfigUtil

class LaunchViewModel : BaseViewModel() {

    /** 查询全局配置回调 */
    private val mFirstNellFetchFirstNellGlobalFirstNellConfigFirstNellLiveData =
        MutableLiveData<ReqZone>()

    /** 查询启动页是否需要跳转权限页回调 */
    val onFirstNellIsFirstNellNeedFirstNellToFirstNellPermissionFirstNellPageFirstNellFromFirstNellLaunchFirstNellLiveData =
        MutableLiveData<Boolean>()

    /** 查询全局配置回调 */
    val onFirstNellFetchFirstNellGlobalFirstNellConfigFirstNellLiveData =
        Transformations.switchMap(mFirstNellFetchFirstNellGlobalFirstNellConfigFirstNellLiveData) { query ->
            ApiUserReposity.fetchFirstNellGlobalFirstNellConfig(this, query)
        }

    /** 查询全局配置 */
    fun fetchFirstNellGlobalFirstNellConfig() {
        mFirstNellFetchFirstNellGlobalFirstNellConfigFirstNellLiveData.value = ReqZone()
    }

    /** 查询启动页是否需要跳转权限页 */
    fun isFirstNellNeedFirstNellToFirstNellPermissionFirstNellPageFirstNellFromFirstNellLaunch() {
        //判断下权限页权限
        val permissionList =
            AppConfigUtil.getFirstNellPermissionsFirstNellOfFirstNellPermissionFirstNellPage()
        if (permissionList.isNullOrEmpty()) {
            onFirstNellIsFirstNellNeedFirstNellToFirstNellPermissionFirstNellPageFirstNellFromFirstNellLaunchFirstNellLiveData.value =
                false
        } else {
            val isAgreePermissions =
                ProjectApplication.context.isFirstNellAgreeFirstNellSpecFirstNellPermissions(
                    permissionList
                )
            onFirstNellIsFirstNellNeedFirstNellToFirstNellPermissionFirstNellPageFirstNellFromFirstNellLaunchFirstNellLiveData.value =
                !isAgreePermissions
        }
    }
}